﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BUS;
using TuyenDungViecLam.CommonResource;
namespace TuyenDungViecLam.User_control
{
    public partial class Detail_Major : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                LienKetDuLieu();
            }
        }

        protected void dscongviec_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "chon")
            {
                for (int i = 0; i < dscongviec.Rows.Count; i++)
                {
                    CheckBox cbchon = (CheckBox)dscongviec.Rows[i].FindControl("cbchon");
                    cbchon.Checked = true;
                }
            }
        }



        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "NhaTuyenDung" || Session["Role"].ToString() == "Administrator")
            {
                Button3.Visible = false;
            }
        }

        public void LienKetDuLieu()
        {
            string Mng = Request.QueryString["Mng"];
            if (Mng != null)
            {
                try
                {
                    dscongviec.DataSource = ChiTietCongViec_BUS.DanhSachCacCongViecThuocNganhNghe(Mng);
                    dscongviec.DataBind();
                }
                catch (Exception)
                {
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }

            string Mct = Request.QueryString["Mct"];
            if (Mct != null)
            {
                try
                {
                    dscongviec.DataSource = ChiTietCongViec_BUS.DanhSachCacCongViecThuocNhaTuyenDung(Mct);
                    dscongviec.DataBind();
                }
                catch (Exception)
                {
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
            string Mnlv = Request.QueryString["Mnlv"];
            if (Mnlv != null)
            {
                try
                {
                    dscongviec.DataSource = ChiTietCongViec_BUS.DanhSachCacCongViecThuocNoiLamViec(Mnlv);
                    dscongviec.DataBind();
                }
                catch (Exception)
                {
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
        }

        protected void dscongviec_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dscongviec.PageIndex = e.NewPageIndex;
            LienKetDuLieu();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "NguoiTimViec")
            {
                for (int i = 0; i < dscongviec.Rows.Count; i++)
                {
                    Label lbmacongviec = (Label)dscongviec.Rows[i].Cells[0].FindControl("lbmacongviec");
                    CheckBox cbchon = (CheckBox)dscongviec.Rows[i].Cells[0].FindControl("cbchon");
                    if (cbchon.Checked == true)
                    {
                        NguoiTimViec_BUS.LuuCongViecDaXem(int.Parse(Session["UserID"].ToString()), int.Parse(lbmacongviec.Text), DateTime.Now);
                    }
                }
                string url = "../NguoiTimViec/DanhSachCongViecDaLuu.aspx";
                string Message = "Đã lưu công việc này cho bạn !";
                Alert.ShowAndRedirect(Message, url);
            }
            else
            {
                string Mcv = "";
                for (int i = 0; i < dscongviec.Rows.Count; i++)
                {
                    Label lbmacongviec = (Label)dscongviec.Rows[i].Cells[0].FindControl("lbmacongviec");
                    CheckBox cbchon = (CheckBox)dscongviec.Rows[i].Cells[0].FindControl("cbchon");
                    if (cbchon.Checked == true)
                    {
                        Mcv += lbmacongviec.Text + ",";
                    }
                }
                Mcv = Mcv.Remove(Mcv.Length - 1);
                string url = "DangNhap.aspx?Mcv=" + Mcv;
                string Message = "Vui lòng đăng nhập để lưu lại công việc này cho bạn !";
                Alert.ShowAndRedirect(Message, url);
            }
        }

        
    }
}